USE RapPhim
GO

declare @cursorSuatChieu cursor
set @cursorSuatChieu = cursor for (select MaSuatChieu, MaPhongChieu from SUATCHIEU)

declare @maSuatChieu int
declare @maPhongChieu int

open @cursorSuatChieu
fetch next from @cursorSuatChieu into @maSuatChieu, @maPhongChieu
while (@@fetch_status = 0)
	begin
		declare @soLuongDay int
		declare @soLuongHang int
		set @soLuongDay =  (select SoLuongDay from PHONGCHIEU where MaPhongChieu = @maPhongChieu)
		set @soLuongHang =  (select SoLuongHang from PHONGCHIEU where MaPhongChieu = @maPhongChieu)
		declare @soDay int
		declare @soHang int
		set @soDay = 1
		while (@soDay <= @soLuongDay)
			begin
				set @soHang = 1
				while (@soHang <= @soLuongHang)
					begin
						INSERT INTO VE([MaLoaiVe],[MaSuatChieu],[MaKhuyenMai],[MaTaiKhoan],[TinhTrang],[ChiSoDay],[ChiSoHang],[ThanhTien])
							VALUES (NULL, @maSuatChieu, NULL, NULL, 0, CHAR(@soDay + 64), @soHang, NULL)
						set @soHang = @soHang + 1
					end
				set @soDay = @soDay + 1	
			end
		fetch next from @cursorSuatChieu into @maSuatChieu, @maPhongChieu
	end
close @cursorSuatChieu
deallocate @cursorSuatChieu